package infostroy.bookagolf.controllers;

import infostroy.bookagolf.components.controls.ToggleButton;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;

public class TabsMouseAdapter implements MouseListener {

    private JPanel container;

    private ToggleButton currentTab;

    public TabsMouseAdapter(JPanel container, ToggleButton currentTab) {
        this.container = container;
        this.currentTab = currentTab;
    }

    private Map<ToggleButton, JPanel> tabsMap = new HashMap<>();

    public void addTab(ToggleButton tab, JPanel content) {
        this.tabsMap.put(tab, content);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object object = e.getSource();
        if (object instanceof ToggleButton) {
            if (currentTab != null) {
                currentTab.setActive(false);
                tabsMap.get(currentTab).setVisible(false);
                container.remove(tabsMap.get(currentTab));
            }

            ToggleButton clickedTab = (ToggleButton) object;
            clickedTab.setActive(true);
            clickedTab.repaint();

            tabsMap.get(clickedTab).setVisible(true);
            container.add(tabsMap.get(clickedTab));
            container.revalidate();
            this.currentTab = clickedTab;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
